package com.leyou.item.service;

import com.leyou.common.enums.ExceptionEnum;
import com.leyou.common.exception.LyException;
import com.leyou.common.utils.BeanHelper;
import com.leyou.item.dto.SpecGroupDTO;
import com.leyou.item.dto.SpecParamDTO;
import com.leyou.item.entity.SpecGroup;
import com.leyou.item.entity.SpecParam;
import com.leyou.item.mapper.SpecGroupMapper;
import com.leyou.item.mapper.SpecParamMapper;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
public class SpecService {

    @Autowired
    private SpecGroupMapper groupMapper;

    @Autowired
    private SpecParamMapper specParamMapper;

    // 查询规格组
    public List<SpecGroupDTO> queryGroupByCategory(Long id) {
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(id);
        List<SpecGroup> list = groupMapper.select(specGroup);

        if (CollectionUtils.isEmpty(list)) {
            throw new LyException(ExceptionEnum.BRAND_NOT_FOUND);
        }
        // 对象转换
        return BeanHelper.copyWithCollection(list,SpecGroupDTO.class);
    }

    //新增规格分组
    @Transactional
    public void saveSpecGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = new SpecGroup();
        //specGroup.setCid(cid);
        //specGroup.setName(name);
        BeanUtils.copyProperties(specGroupDTO, specGroup);
        int count = groupMapper.insertSelective(specGroup);
        if (count != 1) {
            // 新增失败，抛出异常
            throw new LyException(ExceptionEnum.INSERT_OPERATION_FAIL);
        }
    }

    //删除规格分组
    @Transactional
    public void deleteSpecGroup(Long id) {
        int count = groupMapper.deleteByPrimaryKey(id);
        if (count != 1) {
            throw new LyException(ExceptionEnum.DELETE_OPERATION_FAIL);
        }
    }

    //修改规格分组
    @Transactional
    public void updateSpecGroup(SpecGroupDTO specGroupDTO) {
        SpecGroup specGroup = new SpecGroup();
        BeanUtils.copyProperties(specGroupDTO, specGroup);
        int count = groupMapper.updateByPrimaryKey(specGroup);
        if (count != 1) {
            throw new LyException(ExceptionEnum.UPDATE_OPERATION_FAIL);
        }
    }

    public List<SpecParamDTO> querySpecParamByCategoryId(Long gid, Long cid, Boolean searching) {
        //确保gid和cid有一个
        if(gid==null && cid==null){
            throw new LyException(ExceptionEnum.INVALID_PARAM_ERROR);
        }
        SpecParam specParam = new SpecParam();
        specParam.setGroupId(gid);
        specParam.setCid(cid);
        specParam.setSearching(searching);

        List<SpecParam> specParamList = specParamMapper.select(specParam);
        if (CollectionUtils.isEmpty(specParamList)) {
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }

        return BeanHelper.copyWithCollection(specParamList,SpecParamDTO.class);
    }

    public List<SpecGroupDTO> findSpecGroupByCid(Long id) {
        //查询specGroupList
        SpecGroup specGroup = new SpecGroup();
        specGroup.setCid(id);
        List<SpecGroup> specGroupList = groupMapper.select(specGroup);
        if(CollectionUtils.isEmpty(specGroupList)){
            throw new LyException(ExceptionEnum.SPEC_NOT_FOUND);
        }
        List<SpecGroupDTO> specGroupDTOList = BeanHelper.copyWithCollection(specGroupList, SpecGroupDTO.class);
        //查询specParam
        List<SpecParamDTO> specParamDTOList = querySpecParamByCategoryId(null, id, null);

        //给SpecGroupDTO对象中的SpecParam封装值
        //我们可以采用流的方式做到直接在java端分组
        Map<Long, List<SpecParamDTO>> paramMap = specParamDTOList.stream().collect(Collectors.groupingBy(SpecParamDTO::getGroupId));
        for (SpecGroupDTO specGroupDTO : specGroupDTOList) {
            specGroupDTO.setParams(paramMap.get(specGroupDTO.getId()));
        }

        //下面的方案因为循环里面有数据库操作，所以不可取
        /*for (SpecGroupDTO specGroupDTO : specGroupDTOS) {
            Long groupId = specGroupDTO.getId();
            List<SpecParamDTO> paramDTOS = findSpecParamsByGid(groupId, null, null);
            specGroupDTO.setParams(paramDTOS);
        }*/
        return specGroupDTOList;
    }
}
